from aiogram_dialog import DialogManager
from fluentogram import TranslatorRunner

from src.application.category.service import CategoryService
from src.application.category.use_cases.filter_categories import FilterCategories
from src.application.server.enums import ServerPrivacy, ServerType


async def categories_getter(dialog_manager: DialogManager, **kwargs):
    proxy_type: str = dialog_manager.dialog_data.get("proxy_type")
    proxy_privacy: str = dialog_manager.dialog_data.get("proxy_privacy")
    provider_id: int = dialog_manager.dialog_data.get("provider_id")
    country_id: int = dialog_manager.dialog_data.get("country_id")

    category_service: CategoryService = dialog_manager.middleware_data.get("category_service")

    locale: TranslatorRunner = dialog_manager.middleware_data.get("locale")

    query = FilterCategories(
        proxy_type=getattr(ServerType, proxy_type),
        proxy_privacy=getattr(ServerPrivacy, proxy_privacy),
        country_id=country_id,
        provider_id=provider_id,
    )
    categories = await category_service.filter_categories(query)

    return {
        "categories": categories,
        "user_language": locale.translators[0].locale,
    }
